﻿<%@ WebHandler Language="C#" Class="ContainerCheckSuspendingVue3PageHandler" %>

/* Description:检验待处理
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan 
 * create Date:2024.8.20
 */

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BusinessRule.DTO;
using DatabaseAccess.DBUtility;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using System.Net;
using BusinessRule.Common;
using System.Linq;

public class ContainerCheckSuspendingVue3PageHandler : IHttpHandler
{
    uLMSCommonIBusiness m_Common = uMESBusinessFactory.instance.GetuLMSCommonObject();
    uLMSCheckIBusiness m_CheckObj = uMESBusinessFactory.instance.GetuLMSCheckObject();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (System.IO.StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                uMESProcessResult r = new uMESProcessResult();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getContainerCheckSuspending":
                        r = getContainerCheckSuspending(jObject);//获取待检验备注信息
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "saveData":
                        r = saveData(jObject);//插入更新待检验备注
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                uMESProcessResult r = new uMESProcessResult() { ReturnData = null, RowCount = "0", Code = "500", Message = e.Message };
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //获取待检验备注信息
    uMESProcessResult getContainerCheckSuspending(JObject obj)
    {
        DataTable dtPostData = JsonConvert.DeserializeObject<DataTable>(obj["postData"].ToString());
        string sql = @"SELECT NVL(ck.notes,'') notes FROM containerchecksuspending ck WHERE ck.containerid = '{0}' AND ck.workflowstepid = '{1}' AND ck.isuse = 0";
        sql = String.Format(sql, dtPostData.Rows[0]["ContainerID"].ToString(), dtPostData.Rows[0]["WorkflowStepID"].ToString());
        DataTable dtRes = DatabaseHelper.Query_ReturnDatTable(sql);
        return new uMESProcessResult() { ReturnData = dtRes.Rows.Count > 0 ? dtRes.Rows[0]["notes"].ToString() : "", RowCount = "0", Code = "200", Message = "请求成功" };
    }

    //插入检验待处理数据
    uMESProcessResult saveData(JObject obj)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(obj["postData"].ToString());
        var notes = obj["notes"].ToString();
        var para = new Dictionary<string, string>();
        foreach (DataRow row in dt.Rows)
        {
            var exuteEntity = new ExcuteEntity("containerchecksuspending", ExcuteEntity.ExcuteTypeEnum.selectAll);
            exuteEntity.WhereFileds = new List<FieldEntity>() {
                new FieldEntity("containerid", row["ContainerID"], FieldEntity.FieldTypeEnum.Str),
                new FieldEntity("workflowstepid", row["WorkflowStepID"], FieldEntity.FieldTypeEnum.Str),
                new FieldEntity("isuse", 0, FieldEntity.FieldTypeEnum.Str)
            };
            var dtRes = DatabaseHelper.QueryDataByEntity(exuteEntity);
            var para2 = new Dictionary<string, string>();
            para2.Add("ContainerID", row["ContainerID"].ToString());
            para2.Add("WorkflowstepID", row["WorkflowstepID"].ToString());
            para2.Add("Notes", notes);
            para2.Add("WorkflowStepName", row["WorkflowStepName"].ToString());
            para2.Add("ContainerName", row["ContainerName"].ToString());
            //保存工序检验待处理备注至containerchecknotes表 add zhunan 2024.1.18
            SaveContainerCheckNotes(para2);
            //如果存在工序备注信息，则更新备注数据 add by zhangrj 20231017
            if (dtRes.Rows.Count > 0)
            {
                exuteEntity = new ExcuteEntity("containerchecksuspending", ExcuteEntity.ExcuteTypeEnum.update);
                exuteEntity.WhereFileds = new List<FieldEntity>() {
                    new FieldEntity("containerid", row["ContainerID"], FieldEntity.FieldTypeEnum.Str),
                    new FieldEntity("workflowstepid", row["WorkflowStepID"], FieldEntity.FieldTypeEnum.Str),
                    new FieldEntity("isuse", 0, FieldEntity.FieldTypeEnum.Str)
                };
                var excuteFieldList = new List<FieldEntity>();
                excuteFieldList.Add(new FieldEntity("notes", notes, FieldEntity.FieldTypeEnum.Str));
                exuteEntity.ExcuteFileds = excuteFieldList;
                DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntity(exuteEntity);
                continue;
            }
            //不存在数据，则插入数据
            para2.Add("EmployeeID", obj["employeeid"].ToString());
            para2.Add("FactoryID", obj["factoryid"].ToString());
            m_CheckObj.InsertContainerCheckSuspending(para2);
        }
        return new uMESProcessResult() { ReturnData = true, RowCount = "0", Code = "200", Message = "提交成功" };
    }

    //保存工序检验待处理备注至containerchecknotes表
    void SaveContainerCheckNotes(Dictionary<string, string> para)
    {
        var sql = "SELECT * FROM containerchecknotes t WHERE t.containerid = '{0}' AND t.workflowstepid = '{1}'";
        sql = String.Format(sql, para["ContainerID"], para["WorkflowstepID"]);
        var dt = DatabaseHelper.Query_ReturnDatTable(sql);
        if (dt.Rows.Count > 0)
        {
            var strUpdateSql = @"UPDATE containerchecknotes SET notes = '{0}',lastchangedate = sysdate
                                 WHERE containerid = '{1}' AND workflowstepid = '{2}'";
            strUpdateSql = String.Format(strUpdateSql, para["Notes"], para["ContainerID"], para["WorkflowstepID"]);
            DatabaseHelper.ExecuteSql(strUpdateSql);
        }
        else
        {
            var strInsertSql = @"INSERT INTO containerchecknotes(containerid,workflowstepid,notes,containername,workflowstepname) VALUES('{0}','{1}','{2}','{3}','{4}')";
            strInsertSql = String.Format(strInsertSql, para["ContainerID"], para["WorkflowstepID"], para["Notes"], para["ContainerName"], para["WorkflowStepName"]);
            DatabaseHelper.ExecuteSql(strInsertSql);
        }
    }
}